﻿using BNS_Education.IBLL;
using BNS_Education.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace BNS_Education.UI.Controllers
{
    public class HomePageController : Controller
    {
        // GET: HomePage
        private IStaffService StaffService = BNS_Education.BLLContainer.Container.Resolve<IStaffService>();
        public ActionResult Index()
        {
            //var cookie = Request.Cookies[FormsAuthentication.FormsCookieName].Value;
            if (HttpContext.Request.IsAuthenticated)
            {
                string username = HttpContext.User.Identity.Name;//获取用户名
                Session["Name"] = username;
                FormsIdentity formsidentity = HttpContext.User.Identity as FormsIdentity; //身份信息
                                                                                          // formsidentity.Ticket;
                                                                                          //登陆过
            }
            return View();
        }
        [HttpGet]
        public ActionResult Logout()
        {
            FormsAuthHelp.RemoveFormsAuthCookie();
            return Redirect("~/Login/Login");
        }
        public ActionResult Pageindex()
        {

            return View();
        }
        /// <summary>
        /// 判断原密码是否正确
        /// </summary>
        /// <param name="Phone"></param>
        /// <param name="Pwd"></param>
        /// <returns></returns>
        public ActionResult PwdQueren(string Phone,string Pwd)
        {
            if (!string.IsNullOrWhiteSpace(Pwd))
            {
                var list = StaffService.GetModels(p => true).ToList();
                var list1 = list.Where(p => p.UserPwd == Pwd && p.UserPhone==Phone).Count();
                if (list1 > 0)
                {
                    return Json(1);
                }
                else
                {
                    return Json(0);
                }
            }
            else
            {
                return Json(-1);
            }
        }
        public ActionResult XiugaiPwd()
        {
            return View();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="Phone"></param>
        /// <param name="Pwd"></param>
        /// <param name="NewPwd"></param>
        /// <param name="affirmPwd"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult XiugaiPwd(string Phone, string Pwd, string NewPwd, string affirmPwd)
        {
            ///判断非空
            if (!string.IsNullOrWhiteSpace(Phone) && !string.IsNullOrWhiteSpace(Pwd) && !string.IsNullOrWhiteSpace(NewPwd) && !string.IsNullOrWhiteSpace(affirmPwd))
            {
                //判断两次密码一致
                if (NewPwd == affirmPwd)
                {
                    //接受值
                    var list = StaffService.GetModels(p => true).ToList();
                    //根据手机号码找到一个集合
                    var result = list.Where(p => p.UserPhone == Phone).ToList();
                    var ids = 0;
                    //循环找出ID值行进修改
                    foreach (var item in result)
                    {
                        ids = item.UserId;
                    }
                    //转化为类
                    User m = new Model.User();
                    m.UserId = ids;
                    m.UserPhone = Phone;
                    m.UserPwd = NewPwd;
                    //判断修改成功
                    if (StaffService.Update(m))
                    {
                        return Json(1);
                    }
                    else
                    {
                        return Json(0);
                    }
                }
                else
                {
                    return Json(-2);
                }
            }



            else
            {
                return Json(-1);
            }

        }
    }
}